<template>
  <div class="app-container">
    <div class="top-bar-container">
      <el-row>
        <el-col :span="16">
          <div class="search-container">
            <search-form @search-submit="getMessageboardList">
              <search-input v-model="listQuery.username" placeholder="建议用户" />
              <search-input v-model="listQuery.content" placeholder="建议内容" />
              <search-date-picker v-model="listQuery.startDateStr" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="建议时间起" />
              <search-date-picker v-model="listQuery.endDateStr" type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="建议时间至" />
              <search-select-dict v-model="listQuery.isShow" :dict="yesNoStatusList" placeholder="是否展示" />
            </search-form>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit stripe highlight-current-row>
      <el-table-column align="center" label="ID">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="建议用户">
        <template slot-scope="{row}">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="建议内容">
        <template slot-scope="{row}">
          <span>{{ row.content }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="联系电话">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="电子邮件">
        <template slot-scope="{row}">
          <span>{{ row.email }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="是否展示">
        <template slot-scope="{row}">
          <el-tag :type="row.isShow === 1 ? 'success' : 'info'">
            <span>{{ row.isShow | showContent }}</span>
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="建议时间">
        <template slot-scope="{row}">
          <span>{{ row.createdTime | formatTime }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="排序">
        <template slot-scope="{row}">
          <span>{{ row.sort }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="回复用户">
        <template slot-scope="{row}">
          <span>{{ row.replyUsername }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="回复内容">
        <template slot-scope="{row}">
          <span>{{ row.replyContent }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="回复时间">
        <template slot-scope="{row}">
          <span>{{ row.replyTime | formatTime }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="360"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button v-show="scope.row.isShow === 0" type="success" size="mini" icon="el-icon-view" title="展示" @click="setShow(scope.$index, 1)">展示</el-button>
            <el-button v-show="scope.row.isShow === 1" type="warning" size="mini" icon="el-icon-view" title="取消展示" @click="setShow(scope.$index, 0)">取消展示</el-button>
            <el-button type="primary" size="mini" icon="el-icon-edit" title="展示" @click="showReplyDialog(scope.$index)">内容回复</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="内容回复"
      :visible.sync="dialogReplyVisible"
      width="500px"
      @close="dialogReplyVisible = false"
    >
      <el-form ref="replyForm" :model="replyForm" :rules="replyRules" class="edit-form" autocomplete="on" label-position="right" label-width="100px">
        <el-form-item prop="replyContent" label="回复内容">
          <el-input v-model="replyForm.replyContent" :rows="5" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogReplyVisible = false">取 消</el-button>
        <el-button type="primary" @click="replySubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="展示"
      :visible.sync="dialogSetVisible"
      width="500px"
      @close="dialogSetVisible = false"
    >
      <el-form ref="setForm" :model="setForm" class="edit-form" autocomplete="on" label-position="right" label-width="100px">
        <el-form-item prop="sort" label="排序">
          <el-input-number v-model="setForm.sort" :min="0" :max="1000" :step="1" label="排序值" />（数值大靠前）
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogSetVisible = false">取 消</el-button>
        <el-button type="primary" @click="setSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getMessageboardList" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination/index'
import SearchForm from '@/components/SearchForm/index'
import SearchInput from '@/components/SearchForm/SearchInput/index'
import SearchSelectDict from '@/components/SearchForm/SearchSelectDict'
import SearchDatePicker from '@/components/SearchForm/SearchDatePicker'
import { messageboardList, showMessageboard, replyMessageboard } from '@/api/rest/cms/messageboard'
import { yesNoStatusList } from '@/utils/dict'
import { formatDateTime } from '@/utils/formatdate'
import { elRequiredRule } from '@/utils/el-validator'
let that
export default {
  name: 'CmsQuestion',
  components: { SearchInput, Pagination, SearchForm, SearchSelectDict, SearchDatePicker },
  filters: {
    formatTime(val) {
      if (val) {
        return formatDateTime(val)
      }
    },
    showContent(val) {
      let name
      that.yesNoStatusList.filter(e => {
        if (e.value === val) {
          name = e.name
        }
      })
      return name
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        id: null,
        username: null,
        content: null,
        isShow: null,
        startDateStr: null,
        endDateStr: null,
        replyUsername: null,
        replyTime: null,
        type: 2,
        page: 1,
        limit: 10
      },
      replyForm: {
        id: null,
        replyContent: null
      },
      setForm: {
        id: null,
        sort: 0,
        isShow: 0
      },
      ancestors: null,
      yesNoStatusList: yesNoStatusList,
      dialogReplyVisible: false,
      dialogSetVisible: false,
      replyRules: {
        replyContent: elRequiredRule
      }
    }
  },
  watch: {
    $route(to, from) {
      this.getMessageboardList()
    }
  },
  beforeCreate() {
    that = this
  },
  created() {
    this.getMessageboardList()
  },
  methods: {
    getMessageboardList() {
      this.listLoading = true
      messageboardList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    setShow(index, isShow) {
      const { id, sort } = this.list[index]
      this.setForm.id = id
      this.setForm.sort = sort
      this.setForm.isShow = isShow
      if (isShow === 1) {
        this.dialogSetVisible = true
      } else {
        this.setSubmit()
      }
    },
    setSubmit() {
      showMessageboard(this.setForm).then(response => {
        this.dialogSetVisible = false
        this.$message({
          message: '设置成功',
          type: 'success'
        })
        this.getMessageboardList()
      })
    },
    showReplyDialog(index) {
      this.dialogReplyVisible = true
      const { id, replyContent } = this.list[index]
      this.replyForm = {
        id: id,
        replyContent: replyContent
      }
    },
    replySubmit() {
      this.$refs.replyForm.validate(valid => {
        if (valid) {
          replyMessageboard(this.replyForm).then(response => {
            this.dialogReplyVisible = false
            this.$message({
              message: '添加回复内容成功',
              type: 'success'
            })
            this.getMessageboardList()
          })
        }
      })
    }
  }
}
</script>
<style scoped>
</style>

